import type { Interaction } from './types'
import { emptyInteraction } from './empty'
import { map } from '../map/core'
import { NormalInteraction } from './normal'
import { showInteractionTip } from './interaction-tip'

let currentInteraction = emptyInteraction
let normalInteraction = new NormalInteraction()

export function setInteraction(i: Interaction) {
  if (currentInteraction === i) {
    return
  }
  const i0 = currentInteraction
  i0.detach(map)
  currentInteraction = i
  i.attach(map)
  const { tip } = i
  showInteractionTip(tip)
}

export function setInteractionNone() {
  setInteraction(emptyInteraction)
}

export function setInteractionNormal() {
  setInteraction(normalInteraction)
}

export function initInteractions() {
  map.on('mouseenter', (ev) => currentInteraction.onMouseEnter(ev))
  map.on('mouseleave', (ev) => currentInteraction.onMouseLeave(ev))
  map.on('mousemove', (ev) => currentInteraction.onMouseMove(ev))
  map.on('mousedown', (ev) => currentInteraction.onMouseDown(ev))
  map.on('mouseup', (ev) => currentInteraction.onMouseUp(ev))
  map.on('wheel', (ev) => currentInteraction.onWheel(ev))
  map.getCanvas().addEventListener('keydown', (ev) => currentInteraction.onKeyDown(ev))
  setInteractionNormal()
}
